from django.db import models


# 用户登陆账号的类
# 登陆账号，密码，姓名，状态(启用，禁用)，手机号码，邮箱地址，创建时间，上次登陆时间
class Login(models.Model):
    loginid = models.CharField(verbose_name="登陆账号", max_length=100, primary_key=True, null=False)
    loginpwd = models.CharField(verbose_name="登陆密码", max_length=255, null=False)
    name = models.CharField(verbose_name="姓名", max_length=100, null=False)
    status = models.BooleanField(verbose_name="状态", default=False, null=False)
    mobile = models.CharField(verbose_name="手机号码", max_length=100, default="")
    email = models.EmailField(verbose_name="邮箱地址", max_length=100, default="")
    create_date = models.DateTimeField(verbose_name="创建时间", null=True, default=None)  # 账号创建时间 （系统自动填充）
    last_login = models.DateTimeField(verbose_name="上次登陆时间", null=True, default=None)  # 上次登陆时间（系统自动填充）

    class Meta:
        managed = True
        app_label = "userweb"
        db_table = "User_Login"
        verbose_name = "Login"
        verbose_name_plural = "Login"

    def __str__(self):
        return "%s" % (self.name)


# 角色
# 字段有：Id ,name , desc
class Role(models.Model):
    name = models.CharField(verbose_name="角色名称", max_length=50, unique=True)
    login = models.ManyToManyField(verbose_name="登陆账号", to=Login, null=True, blank=True)
    desc = models.CharField(verbose_name="描述", max_length=255, null=True, blank=True, default=None)

    class Meta:
        managed = True
        app_label = "userweb"
        db_table = "User_Role"
        verbose_name = "Role"
        verbose_name_plural = "Role"

    def __str__(self):
        return "%s" % (self.name)


# 菜单
# 字段有：id, title, icon
class Menu(models.Model):
    title = models.CharField(verbose_name='标题', max_length=50, unique=True)
    icon = models.CharField(verbose_name='标题', max_length=50)

    class Meta:
        managed = True
        app_label = "userweb"
        db_table = "User_Menu"
        verbose_name = "Menu"
        verbose_name_plural = "Menu"

    def __str__(self):
        return "%s" % (self.title)

# 权限
# 字段有： Id , title, url
class Permission(models.Model):
    title = models.CharField(verbose_name='标题', max_length=50, unique=True)
    url = models.CharField(verbose_name='URL', max_length=200, unique=True)
    role = models.ManyToManyField(verbose_name="角色", to=Role, null=True, blank=True, default=None)
    menu = models.ForeignKey(verbose_name="所属菜单", to=Menu,  on_delete=models.PROTECT, null=True, blank=True)
    order = models.IntegerField(verbose_name="排序数字", null=True, blank=True, default=0)

    class Meta:
        managed = True
        app_label = "userweb"
        db_table = "User_Permission"
        verbose_name = "Permission"
        verbose_name_plural = "Permission"

    def __str__(self):
        return "%s" % (self.title)

